package com.wbp.cms.core.product.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Preconditions;
import com.wbp.cms.client.product.domain.ProductDO;
import com.wbp.cms.client.product.domain.ProductProfitDO;
import com.wbp.cms.client.product.query.ProductQuery;
import com.wbp.cms.client.product.service.ProductProfitService;
import com.wbp.cms.client.product.service.ProductService;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.core.exception.ServiceException;
import com.wbp.cms.core.product.dao.ProductDAO;


/**
 * @Description:
 * @Department:
 * @author andy.lhc
 * @date 2016年3月9日 下午2:24:20
 */
@Service("productService")
public class ProductServiceImpl implements ProductService {

  
  /**
   * 
   */
  private static final long serialVersionUID = 3413510601623729413L;

  private static final Logger  logger = Logger.getLogger(ProductServiceImpl.class) ;
  
  @Resource
  private ProductProfitService  productProfitService ;

  @Resource
  private ProductDAO   productDAO ;
  
  @Override
  @Transactional
  public Result<Long> addProduct(ProductDO productDO) {
    
          // 参数的校验放在controller层去做,service层只做必要的参数验证
          Preconditions.checkArgument(productDO != null, "productDO参数为空");
          // Preconditions.checkArgument(productDO.getProductProfitList() != null, "ProductProfitList参数为空");
          Preconditions.checkArgument(productDO.getProjectTotalAmount() >0, "ProjectTotalAmount 应>0"); 
          Preconditions.checkArgument(productDO.getSubscribePoint() >0, "SubscribePoint 应>0");
          
          
          productDO.setSurplusAmount(productDO.getProjectTotalAmount());
          Long  productId = productDAO.insert(productDO) ;
          
          if(productDO.getProductProfitList()  !=null && !productDO.getProductProfitList().isEmpty() ){
            List<ProductProfitDO> ppList = productDO.getProductProfitList() ;
            for(ProductProfitDO pp : ppList)
               pp.setProductId(productId);
            
            Result<Integer>  ppResult = productProfitService.addProductProfit(ppList) ;
            if(ppResult.isFailed() || ppResult.getData() != ppList.size() )
                 throw new ServiceException(ppResult.getErrMsg());
          }
               
         return Result.buildSuccess(productId);
    
  }

   
  @Override
  @Transactional
  public Result<Boolean> modifyProduct(ProductDO productDO) {
    
    Preconditions.checkArgument(productDO != null, "productDO参数为空"); 
    int  updateResult = productDAO.update(productDO) ;
    
    if(updateResult <=0)
       return Result.buildError("修改product失败:productId:"+productDO.getId()) ;
    
    List<ProductProfitDO> ppList = productDO.getProductProfitList() ;
    if(ppList !=null && !ppList.isEmpty() ){
         this.productProfitService.removeProductProfit(productDO.getId()) ;  // 先删除
         
         for(ProductProfitDO pp : ppList)
           pp.setProductId(productDO.getId()); 
         
         Result<Integer>  ppResult = productProfitService.addProductProfit(ppList) ;  //再新增
         if(ppResult.isFailed() || ppResult.getData() != ppList.size() )
              throw new ServiceException(ppResult.getErrMsg());
    }
    return Result.buildSuccess(updateResult > 0 );
  }

  @Override
  public Result<Boolean>  updateProduct(ProductDO productDO){
    Preconditions.checkArgument(productDO != null, "productDO参数为空"); 
    int  updateResult = productDAO.update(productDO) ;
    
    if(updateResult <=0)
       return Result.buildError("修改product失败:productId:"+productDO.getId()) ;
    return Result.buildSuccess(updateResult > 0 );
  }
   
  @Override
  public Result<ProductDO> loadProduct(Long productId) {
    try {
          Preconditions.checkNotNull(productId, "productId:"+productId+",不能为空") ;

          ProductDO  product = productDAO.load(productId) ;
          if(product !=null   ){
             Result<List<ProductProfitDO>> productProfit = productProfitService.loadProductProfitByProductId(product.getId()) ; 
             if(productProfit !=null)
               product.setProductProfitList(productProfit.getData());
          }
          return Result.buildSuccess(product);
        } catch (Exception e) {
             logger.error("loadProductProfit exception,productId:" + productId, e);
             return Result.buildError(e.getMessage());
        }
  }

  
  @Override
  public Result<List<ProductDO>> queryProduct(ProductQuery productQuery) {
     
    Preconditions.checkArgument(productQuery !=null , "productQuery 查询参数不能为null"); 
    
    int count  = productDAO.queryCount(productQuery) ;
    if(count >0){
      List<ProductDO>  list =   productDAO.query(productQuery) ;
      if(list !=null && !list.isEmpty() ){
           for(ProductDO p : list){
               Result<List<ProductProfitDO>> productProfit = productProfitService.loadProductProfitByProductId(p.getId()) ; 
                if(productProfit !=null)
                    p.setProductProfitList(productProfit.getData());
           }
      }
      return  Result.buildSuccess(count, list) ;
    }
    logger.error("productQuery:"+productQuery+",没有查到产品数据");
    return Result.buildError("没有查到产品数据");
  }

  
   
}
